package com.hello.web;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hello.config.ConfigDef;
import com.hello.config.DisplayGroup;
import com.hello.config.PropertyDef;
import com.hello.config.RuntimeConfigs;
import com.hello.service.MarshallingService;

@Controller
@RequestMapping("/page.html")
public class PageController {
	
	private MarshallingService marshallingService;
	
	@Autowired
	public void setMarshallingService(MarshallingService marshallingService) {
		this.marshallingService = marshallingService;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String showPage() throws IOException {
		
		PropertyDef pt1 = new PropertyDef();
		pt1.setName("site.name");
		pt1.setKey("configForm.siteName");
		pt1.setType("String");
		pt1.setDefaultValue("Roller Site");
		PropertyDef pt2 = new PropertyDef();
		pt2.setName("site.shortName");
		pt2.setKey("configForm.shortName");
		pt2.setType("String");
		pt2.setDefaultValue("Front Page");
		DisplayGroup dg1 = new DisplayGroup();
		dg1.setName("siteSettings");
		dg1.setKey("configForm.siteSettings");
		dg1.addPropertyDef(pt1);
		dg1.addPropertyDef(pt2);
		PropertyDef pt3 = new PropertyDef();
		pt3.setName("users.registration.enabled");
		pt3.setKey("configForm.allowNewUsers");
		pt3.setType("boolean");
		pt3.setDefaultValue("true");
		DisplayGroup dg2 = new DisplayGroup();
		dg2.setName("userSettings");
		dg2.setKey("configForm.userSettings");
		dg2.addPropertyDef(pt3);
		ConfigDef cd = new ConfigDef();
		cd.setName("global-properties");
		cd.addDisplayGroup(dg1);
		cd.addDisplayGroup(dg2);
		RuntimeConfigs rc = new RuntimeConfigs();
		rc.addConfigDef(cd);
		
		this.marshallingService.saveConfiguration("testxml", rc);
		rc = (RuntimeConfigs) this.marshallingService.loadConfiguration("testxml");
		System.out.println(rc);
		
		return "page";
	}
}
